package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageReturn;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告管理（后台）/4.1、广告位管理
 */
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {
    @Autowired
    private IYddAdvposService yddAdvposService;

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    /**
     * 广告位列表分页查询
     *
     * @param yddAdvposDto
     * @return
     */
    @GetMapping("/list")
    public ResultData list(YddAdvposDto yddAdvposDto) {
        //分页拦截器
        Page<YddAdvpos> page = new Page<>(yddAdvposDto.getPage(), yddAdvposDto.getLimit());
        //条件构造器
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(yddAdvposDto.getAdvposname()), YddAdvpos::getAdvposname, yddAdvposDto.getAdvposname());
        queryWrapper.like(StringUtils.isNotEmpty(yddAdvposDto.getAdvposdesc()), YddAdvpos::getAdvposdesc, yddAdvposDto.getAdvposdesc());
        //查询
        Page<YddAdvposDto> yddAdvposDtoPage = yddAdvposService.pageByCondition(page, queryWrapper);
        //数据封装返回
        PageReturn<YddAdvposDto> change = PageReturn.change(yddAdvposDtoPage);
        return ResultData.ok("", change);
    }

    /**
     * 广告位修改页面回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id) {
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        return ResultData.ok("", yddAdvpos);
    }

    /**
     * 广告位修改
     *
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos) {
        yddAdvposService.updateById(yddAdvpos);
        return ResultData.ok("修改成功", null);
    }

    /**
     * 广告位删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(int id) {
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddAdvimg::getAdvimgpos, id);
        List<YddAdvimg> list = yddAdvimgService.list(queryWrapper);
        if (list.size()>0) {
            return ResultData.error("请先删除关联广告图");
        }
        yddAdvposService.removeById(id);
        return ResultData.ok("删除成功", null);
    }

    /**
     * 广告位列表-全部
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData all() {
        List<YddAdvpos> list = yddAdvposService.list();
        return ResultData.ok("", list);
    }

    /**
     * 广告位添加
     *
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddAdvpos yddAdvpos) {
        yddAdvposService.save(yddAdvpos);
        return ResultData.ok("添加成功", null);
    }
}
